package com.empsys.controller;

import com.empsys.pojo.Result;
import com.empsys.utils.AliyunOssUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;
import java.util.logging.Filter;

/**
 * @author wzm
 * @create 2023-12-11 11:20
 * project_name:web2310
 * @Description
 */
@Slf4j
@RestController
public class UploadController {

    //注入阿里云ossutils
    @Autowired
    AliyunOssUtils aliyunOssUtils;



    //文件上传的方法
    //本地
    /*MultipartFile Spring提供的接收文件上传的api*/
/*
    @PostMapping("/upload")
    public Result upload(String username,Integer age, MultipartFile image) throws IOException {
        //获取原始文件名
        String originalFilename = image.getOriginalFilename();
        log.info("文件上传：{}，{}，{},{}",username,age,image,originalFilename);//值班.png

        //uuid
        String uuid = UUID.randomUUID().toString();
        //从原始名字中获取文件的后缀名  截取最后一个.后面的
        String lastName =uuid + originalFilename.substring(originalFilename.lastIndexOf('.'));//扩展名


        //在程序返回之前将上传的文件转移到磁盘目录
        //避免名词重复使用UUID+后缀名
        image.transferTo(new File("D:/pics/"+lastName));

        return Result.success(lastName);


    }
*/

    //阿里云
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {

        //上传文件到阿里云oss
        String url = aliyunOssUtils.upload(image);
        //上传图片完毕后返回图片的url地址
        return Result.success(url);

    }

}
